package com.leetcode.partition8;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/9 22:15
 */
public class LC798得分最高的最小轮调 {

    private static final int N = 100010;
    private static int[] s = new int[N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int[] a = new int[ss.length];
        for (int i = 0; i < ss.length; i++) a[i] = Integer.parseInt(ss[i]);
        writer.write(bestRotation(a) + "\n");
        writer.flush();
    }

    public static int bestRotation(int[] a) {
        //https://leetcode-cn.com/problems/smallest-rotation-with-highest-score/solution/gong-shui-san-xie-shang-xia-jie-fen-xi-c-p6kh/
        n = a.length;
        for (int i = 0; i < n; i++) {
            //当前在哪个区间范围内有效
            int l = getMod(i - (n - 1), n), r = getMod(i - a[i], n);
            if (l <= r) {
                add(l, r, 1);
            } else {
                add(0, r, 1);
                add(l, n - 1, 1);
            }
        }
        for (int i = 1; i <= n; i++) s[i] += s[i - 1];
        //统计有效最多的区间
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            if (s[i] > s[ans]) ans = i;
        }
        return ans;
    }

    private static void add(int l, int r, int k) {
        s[l] += k;
        s[r + 1] -= k;
    }

    private static int getMod(int a, int b) {
        return (a % b + b) % b;
    }
}
